if(typeof(window.external) != 'undefined')
{
  document.getElementsByName = function(name, tag)
   {
    if(!tag){
        tag = '*';
    }
    var elems = document.getElementsByTagName(tag);
    var res = []
    for(var i = 0; i < elems.length; i++)
      {
        att = elems[i].getAttribute('name');
        if(att == name) 
          {
            res.push(elems[i]);
          }
      }
 
   return res;
  }
}

var speed_factor = 1.0;

function faster ()
{
  if (speed_factor > 0.3) 
    { 
      speed_factor -= 0.2; 
      document.getElementById("speed-factor").innerHTML = speed_factor;
    }
}

function slower ()
{
  if (speed_factor < 10)
   { 
     speed_factor += 0.2; 
     document.getElementById("speed-factor").innerHTML = speed_factor;
   }
}

function update (box_name, name, cnt, time_step)
{
  document.getElementById(box_name).innerHTML = "";
  
  var ps = document.getElementsByName (name);
  var n = ps [cnt];
  var t;

  if (!n.getAttribute ("id"))
    t = time_step;
  else
    t = n.id;

  cnt ++;
  if (cnt == ps.length)
    cnt = 0;

  var nn = n.cloneNode (true);
  nn.getAttribute ("name").nodeValue = "";
  // nn.style.visibility = 'visible';
  nn.style.display = 'inline';
  
  document.getElementById(box_name).appendChild(nn);
  
  setTimeout("update(\'" + box_name + "\', \'" + name + "\'," + cnt + "," + time_step + ")", t * speed_factor);
}

function hide_ps (tag_name)
{
 var i;
 var ps1 = document.getElementsByTagName (tag_name);

 for (i = 0; i < ps1.length; i++)
 {
     // ps1[i].style.visibility = 'hidden';
     ps1[i].style.display = 'none';
 }
}

function get_h_boxes (h_boxes_name)
{
    var i;
    var h_boxes = new Array ();
    var h_names = h_boxes_name.split (";");

    for (i = 0; i < h_names.length; i++)
	{
	    h_boxes.push (document.getElementById (h_names[i]));
	}

    return h_boxes;
}

function get_child_by_name (elem, ch_name)
{
    var i;

    for (i = 0; i < elem.childNodes.length;i++)
	{
	    try
		{
		    var n = elem.childNodes [i];
		    var attr = n.getAttribute ('name');
		    if (ch_name == attr)
			{
			    return elem.childNodes [i];
			}
		}
	    catch (err)
		{}
	}

    return null;
}

function h_view (v_elem_name, h_boxes_name)
{
    var v_elem = document.getElementById (v_elem_name);
    var boxes = get_h_boxes (h_boxes_name);
    var i;
    var asoc = (v_elem_name + ";" + get_child_by_name (v_elem, "asoc").id).split (";");

    for (i = 0; i < boxes.length; i++)
	{
	    boxes [i].innerHTML = "";

	    if (i < asoc.length)
		{
		    var type = boxes[i].getAttribute ('name');
		    var n = document.getElementById (asoc [i]);
		    var nn = get_child_by_name (n, type);
	    
		    var nnn = nn.cloneNode (true);
		    nnn.getAttribute ('name').nodeValue = "";
		    nnn.setAttribute ('id', n.getAttribute ('id') + "_ID");
		    // nn.style.visibility = 'visible';
		    nnn.style.display = 'inline';
	      
		    boxes[i].appendChild(nnn);
		}
	}
}

function h_select (e, h_boxes_name)
{
    var targ;

    if (!e)
	{
	    var e = window.event;
	}
    if (e.target)
	{
	    targ = e.target;
	}
    else if (e.srcElement)
	{
	    targ = e.srcElement;
	}
    if(targ.nodeType == 3) // defeat Safari bug
	{
	    targ = targ.parentNode;
	}

    if (targ.getAttribute ('id'))
	{
	    var name = targ.childNodes [0].getAttribute ('id');
	    name = name.substr (0, name.length - 3);
	    h_view (name, h_boxes_name);
	}
}